<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of FeedbackController
 *
 * @author thanhlnh
 */
class FeedbackController extends Zend_Controller_Action{
    
    public function __construct(Zend_Controller_Request_Abstract $request, Zend_Controller_Response_Abstract $response, array $invokeArgs = array()) {
        parent::__construct($request, $response, $invokeArgs);
    }
    
    public function checkAccessKeyParams() {
        $appid = $this->_request->getParam("key");
        $sig = $this->_request->getParam("sig");
        $debug = $this->_request->getParam("db");
        if ($debug == '1')
            return true;

        if ($appid == "" || $sig == "") {
            echo Sunny_ErrorHandled::handleError(Sunny_ErrorHandled::$INVAILD_PARAMS);
            return false;
        } else {

            if ($appid != Haibl_ConfigData::KEY && $appid != '256845' && $appid != '74523') {
                echo Sunny_ErrorHandled::handleError(Sunny_ErrorHandled::$NOT_FOUND_APIKEY);
                return false;
            } else {
                $params = $this->_request->getParams();
                $requestParams = '';
                foreach ($params as $key => $value) {
                    if ($key != 'controller' && $key != 'action' && $key != 'module' && $key != "sig") {
                        $requestParams.= $key . '=' . $value;
                    }
                }
                $requestParams.= Haibl_ConfigData::ACCESS_KEY;
                $encryptParams = md5($requestParams);

                if ($encryptParams == $sig) {
                    return true;
                } else {
                    echo Sunny_ErrorHandled::handleError(Sunny_ErrorHandled::$NOT_FOUND_APIKEY);
                    if (Haibl_ConfigData::DEBUG) {
                        echo "<p>requestParams = " . $requestParams . "</p>";
                        echo "<p>encryptParams = " . $encryptParams . "</p>";
                    }
                    return false;
                }
            }
        }
    }
    
    public function addAction() {
        $this->_helper->viewRenderer->setNoRender(true);        
        if ($this->checkAccessKeyParams()) {
            
            $user_id = $this->getRequest()->getPost('id', null);
            $fback = $this->getRequest()->getPost('feedback', null);

            $user = new Model_Userreg();
            $user_login = $user->get_user_by_id($user_id);
            if (empty($user_login)) {
                echo Sunny_ErrorHandled::handleError(Sunny_ErrorHandled::$USER_NOT_FOUND);
            } else {
                $data = array(
                    'user_id' => $user_id,
                    'user_feedback' => $fback
                );
                $feedback = new Model_Feedback();
                $id = $feedback->insert_new_feedback($data);

                if ($id != -1) {
                    $returnData['errorId'] = Sunny_ErrorHandled::$DONE;
                    $returnData['data']['feed_id'] = $id;
                    echo json_encode($returnData);
                } else {
                    echo Sunny_ErrorHandled::handleError(Sunny_ErrorHandled::$NO_DATA_RECORD);
                }
            }
        }
    }

    public function getAction() {
        $this->_helper->viewRenderer->setNoRender(true);
        $id = $this->_request->getParam("id");
        $feedback = new Model_Feedback();
        $data = $feedback->get_feed_by_id($id);
        echo print_r($data);
    }

}

?>
